<?php
	require_once ("../../config.inc.php");
	require_once ("../../include/mysql.inc.php");
	require_once ("../../include/articleMsg.inc.php");
	require_once ("../../include/util.inc.php");
	require_once ('../../util/emailUtil.php');
	
	session_start();
	$prevTime = $_SESSION['operaTime'];
	$currTime = time();
	$dom = new DOMDocument("1.0","UTF-8");
	$result = '';
	$timeSep = $currTime - $prevTime;
	if ((!empty($prevTime)) && ($timeSep < MSG_TIME_SEP)) {
		$result = $dom->createElement("result", MSG_TIME_SEP - $timeSep);//间隔太短，不允许发，稍后再试
	}
	else {
		$_SESSION['operaTime'] = $currTime;
		
		$name = mysql_escape_string($_POST ["name"]);
		$content = htmlentities(mysql_escape_string($_POST ["content"]), ENT_NOQUOTES, "UTF-8");
		$ip = Util::getIp();
		$articleId = $_POST ["articleId"];
		
		$articleMsgDao = new ArticleMsg();
		
		$flag = $articleMsgDao->add($name, $content, $ip, $articleId);
		
		if ($flag) {
			$result = $dom->createElement("result", 0);//成功
			Email::tipEmailToAdmin('主页有新的博客评论，赶紧处理！', $name .'<br />' .$content . '<br />' . $ip . '<br />' . $articleId);
		}
		else {
			$result = $dom->createElement("result", -1);//失败
			$_SESSION['operaTime'] = $currTime - MSG_TIME_SEP;
		}
	}
	
	$dom->appendChild($result);
	header("Content-type:text/xml");
	$xmlfile = $dom->saveXML();
	file_put_contents("../../xml_data/msgAdd.xml",$xmlfile);
	echo $xmlfile;
?>